package net.maku.needy.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.needy.convert.NeedyMemberConvert;
import net.maku.needy.entity.NeedyMemberEntity;
import net.maku.needy.entity.NeedyMemberFamilyEntity;
import net.maku.needy.service.NeedyMemberFamilyService;
import net.maku.needy.service.NeedyMemberService;
import net.maku.needy.query.NeedyMemberQuery;
import net.maku.needy.vo.NeedyMemberVO;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
* 困难职工
*
* @author 阿沐 babamu@126.com
* @since 1.0.0 2025-03-19
*/
@RestController
@RequestMapping("needy/needy_member")
@Tag(name="困难职工")
@AllArgsConstructor
public class NeedyMemberController {
    private final NeedyMemberService needyMemberService;

    private final NeedyMemberFamilyService needyMemberFamilyService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('needy:needy_member:page')")
    public Result<PageResult<NeedyMemberVO>> page(@ParameterObject @Valid NeedyMemberQuery query){
        PageResult<NeedyMemberVO> page = needyMemberService.page(query);

        return Result.ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('needy:needy_member:info')")
    public Result<NeedyMemberVO> get(@PathVariable("id") Long id){
        NeedyMemberEntity entity = needyMemberService.getById(id);
        NeedyMemberVO needyMember = NeedyMemberConvert.INSTANCE.convert(entity);
        List<NeedyMemberFamilyEntity> needyMemberFamilyEntities = needyMemberFamilyService.selectMemberFamilyList(needyMember.getMemberId());
        needyMember.setMemberFamilyList(needyMemberFamilyEntities);
        return Result.ok(needyMember);
    }

    @PostMapping
    @Operation(summary = "保存")
    @PreAuthorize("hasAuthority('needy:needy_member:save')")
    public Result<String> save(@RequestBody NeedyMemberVO vo){
        needyMemberService.save(vo);

        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    @PreAuthorize("hasAuthority('needy:needy_member:update')")
    public Result<String> update(@RequestBody @Valid NeedyMemberVO vo){
        needyMemberService.update(vo);

        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('needy:needy_member:delete')")
    public Result<String> delete(@RequestBody List<Long> idList){
        needyMemberService.delete(idList);

        return Result.ok();
    }
}